package cn.edu.zjicm.wordsnet_d.BroadcastReceiver;

import java.util.List;

import android.content.Context;

import com.xiaomi.mipush.sdk.ErrorCode;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import com.xiaomi.mipush.sdk.MiPushMessage;
import com.xiaomi.mipush.sdk.PushMessageReceiver;

/**
 * 1.onCommandResult和onReceiveMessage方法运行在非UI线程中。
 * 2.如果你的应用使用了混淆，你需要keep自定义的BroadcastReceiver:
 * #这里com.xiaomi.mipushdemo.DemoMessageRreceiver改成app中定义的完整类名 -keep class
 * com.xiaomi.mipush.sdk.DemoMessageReceiver {*;}
 * 
 * @author cnting
 * 
 */
public class MiPushReceiver extends PushMessageReceiver {

	private String mRegId;
	private long mResultCode = -1;
	private String mReason;
	private String mCommand;
	private String mMessage;
	private String mTopic;
	private String mAlias;
	private String mStartTime;
	private String mEndTime;

	private String TAG = "小米推送";

	@Override
	public void onCommandResult(Context arg0, MiPushCommandMessage message) {
		String command = message.getCommand();
		List<String> arguments = message.getCommandArguments();
		String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
		String cmdArg2 = ((arguments != null && arguments.size() > 1) ? arguments.get(1) : null);
		if (MiPushClient.COMMAND_REGISTER.equals(command)) {
			if (message.getResultCode() == ErrorCode.SUCCESS) {
				mRegId = cmdArg1;
			}
		} else if (MiPushClient.COMMAND_SET_ALIAS.equals(command)) {
			if (message.getResultCode() == ErrorCode.SUCCESS) {
				mAlias = cmdArg1;
			}
		} else if (MiPushClient.COMMAND_UNSET_ALIAS.equals(command)) {
			if (message.getResultCode() == ErrorCode.SUCCESS) {
				mAlias = cmdArg1;
			}
		} else if (MiPushClient.COMMAND_SUBSCRIBE_TOPIC.equals(command)) {
			if (message.getResultCode() == ErrorCode.SUCCESS) {
				mTopic = cmdArg1;
			}
		} else if (MiPushClient.COMMAND_UNSUBSCRIBE_TOPIC.equals(command)) {
			if (message.getResultCode() == ErrorCode.SUCCESS) {
				mTopic = cmdArg1;
			}
		} else if (MiPushClient.COMMAND_SET_ACCEPT_TIME.equals(command)) {
			if (message.getResultCode() == ErrorCode.SUCCESS) {
				mStartTime = cmdArg1;
				mEndTime = cmdArg2;
			}
		}
	}

	@Override
	public void onReceiveMessage(Context arg0, MiPushMessage message) {
		mMessage = message.getContent();
		if (message.getTopic() != null && message.getTopic().length() > 0) {
			mTopic = message.getTopic();
		} else if (message.getAlias() != null && message.getAlias().length() > 0) {
			mAlias = message.getAlias();
		}
	}

}
